<?php
/**
 * (exporter)
 */
namespace App\Exporter\Reimburse;

use App\Models\Reimburse\ReimburseApply;
use App\Util\User;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;
class ApplyExporter extends ExcelExporter implements WithMapping
{
    protected $fileName = '返款申请.csv';

    protected $headings =  ['提报时间','审核状态','付款状态','费用类型','刷单类型','订单号/费用描述','提报单号','国家','销售员','亚马逊SKU','公司SKU','产品名称','品类'
,'支付方式','支付账号','返款类型','币种','订单联系人','备注','留评状态','留评链接','留评时间','佣金币种','佣金支付方式','佣金账号'
,'一次性返款','首次返款','二次返款','佣金'];

    public function map($model) : array
    {
        $data1 = [
            $model->created_at,
            ReimburseApply::$_auditStatus[$model->audit_status],
            ReimburseApply::$_paidStatus[$model->paid_status],
            ReimburseApply::$_feeType[$model->fee_type],
            ReimburseApply::$_rType[$model->r_type],
            $model->order_no,
            $model->reimburse_no,
            $model->country->country ?? '',
            User::getUsernameById($model->seller_id),
            $model->seller_sku ?: '',
            $model->sku->psku_code ?? '',
            $model->sku->psku_cn ?? '',
            $model->skuClassify->title ?? '',
            ReimburseApply::$_paidMethod[$model->paid_method],
            $model->paid_account_no,
            ReimburseApply::$_paidType[$model->paid_type],
            $model->currency_code,
            $model->order_atten,
            $model->remarks,
            ReimburseApply::$_reviewStatus[$model->review_status],
            $model->review_link,
            $model->review_at
        ];
        $paid = [1=>[],2=>[],3=>[],4=>[]];$_paid_ = [];
        foreach($model->paid as $item){
            if($item->order_by == 1){
                $paid[1] = $item;
            }elseif($item->order_by == 2){
                $paid[2] = $item;
            }elseif($item->order_by == 3){
                $paid[3] = $item;
            }elseif($item->order_by == 4){
                $paid[4] = $item;
                $_paid_ = $item;
            }
        }
        if(!empty($_paid_)){
            $data2 = [
                $_paid_->currency_code,
                $_paid_->paid_method ? ReimburseApply::$_paidMethod[$_paid_->paid_method] : '',
                $_paid_->paid_account_no ?? '',
            ];
        }else{
            $data2 = [
                '',
                '',
                ''
            ];
        }
        $data3 = [];
        foreach($paid as $k => $item){
            if(!empty($item)){
                if($item->apply_status == 1){
                    $_item = ' 未申请';
                }elseif($item->apply_status == 2 && $item->audit_status == 1){
                    $_item = ' 待审核';
                }elseif($item->audit_status == 2 && $item->paid_status == 1){
                    $_item = ' 待付款';
                }elseif($item->paid_status == 2){
                    $_item = ' 已付款';
                }else{
                    $_item = ' 异常';
                }
                $data3['paid_amount_'.$k] = $item->paid_amount . $_item;
            }else{
                $data3['paid_amount_'.$k] = '';
            }
        }
        return array_merge($data1,$data2,$data3);
    }
}
